package com.haidechizi.i18n.code.generator.common.propconvert.generator;

import com.haidechizi.i18n.code.generator.common.message.language.Language;
import com.haidechizi.i18n.code.generator.common.message.language.LanguageHelper;
import com.haidechizi.i18n.code.generator.common.message.utils.FileTypeUtils;
import com.haidechizi.i18n.code.generator.common.message.utils.FileUtils;
import com.haidechizi.i18n.code.generator.common.message.utils.RegexUtils;
import com.haidechizi.i18n.code.generator.common.propconvert.param.PropConvertConfig;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangdejun
 * @create 2020/10/17 17:55
 */
public abstract class BasePropGenerator implements PropGenerator {

    /**
     * 将所有generator放到工厂中
     */
    @PostConstruct
    public final void initGenerators() {
        if (PropGeneratorFactory.getGenerator(type()) != null) {
            throw new RuntimeException("type() 名称冲突");
        }
        PropGeneratorFactory.addGenerator(type(), this);
    }


    @Override
    public final void generator(PropConvertConfig propConvertConfig) {
        String fileSuffix = FileTypeUtils.fileSuffix(propConvertConfig.getFilePath());
        if(!support(fileSuffix)) {
            throw new RuntimeException("不支持当前类型文件的操作");
        }
        // 读取资源文件的内容
        String resourcesContent = FileUtils.readFile(propConvertConfig.getFilePath());
        resourcesContent = beforeGenerator(propConvertConfig, resourcesContent);
        // 1.将文件转化成key->value
        Map<String, String> messageMap = generatorMessage(propConvertConfig);
        // 获取对应的语言生成类
        List<String> languageTypeList = propConvertConfig.getLanguageTypeList().stream().filter(language ->
                !propConvertConfig.getCurrentLanguage().equals(language)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(languageTypeList)) {
            throw new RuntimeException("无可生成的语言");
        }
        List<Language> languageList = LanguageHelper.getLanguageList(languageTypeList);
        Map<String, Map<String, String>> languageMap = LanguageHelper.batchGenerateLanguage(
                propConvertConfig.getCurrentLanguage(), languageList, messageMap);
        // after
        // 生成对应的文件
        generatorFile(propConvertConfig, languageMap, messageMap, resourcesContent);

    }

    /**
     * 处理资源文件的内容
     *
     * @param propConvertConfig
     * @param resourcesContent
     * @return
     */
    protected String beforeGenerator(PropConvertConfig propConvertConfig, String resourcesContent) {
        return resourcesContent;
    }

    /**
     * 生成国际化资源文件
     * <p>
     * 采用替换字符串的形式，保证新生成的资源文件和原始资源文件格式一致
     *
     * @param propConvertConfig 参数
     * @param languageMap       对应语言的资源内容
     * @param messageMap        原始文件的资源内容
     * @param resourcesContent
     */
    protected final void generatorFile(PropConvertConfig propConvertConfig, Map<String,
            Map<String, String>> languageMap, Map<String, String> messageMap, String resourcesContent) {

//        resourcesContent = FileUtils.unicodeToChina(resourcesContent);
        for (Map.Entry<String, Map<String, String>> entry : languageMap.entrySet()) {
            String content = resourcesContent;
            String filePath = propConvertConfig.getFilePath().replace(propConvertConfig.getCurrentLanguage(), entry.getKey());
            for (Map.Entry<String, String> messageEntry : entry.getValue().entrySet()) {
                String regex = RegexUtils.parseRegex(messageMap.get(messageEntry.getKey()));
                if (messageEntry.getKey().equals("editStoreGroupModal_placeholder_editGroupName")) {
                    System.out.println(11);
                }
                regex = generatorReplaceRegex(regex, messageEntry);
                content = content.replaceAll(regex, generatorReplaceData(messageEntry));
            }
            content = afterGenerator(propConvertConfig, content, entry.getKey());
            FileUtils.createFile(content, filePath, ".new");
        }

    }

    /**
     * 后置处理，处理新生成的资源文件的内容
     *
     * @param propConvertConfig
     * @param content
     * @return
     */
    protected String afterGenerator(PropConvertConfig propConvertConfig, String content,String language) {
        return content;
    }

    /**
     * 生成替换资源文件的正则表达式
     *
     * @param regex
     * @param messageEntry
     * @return
     */
    protected String generatorReplaceRegex(String regex, Map.Entry<String, String> messageEntry) {
        return regex;
    }

    /**
     * 生成替换的内容
     *
     * @param messageEntry
     * @return
     */
    protected String generatorReplaceData(Map.Entry<String, String> messageEntry) {
        return messageEntry.getValue();
    }


    /**
     * 解析静态资源文件，提取国际化数据
     *
     * @param propConvertConfig 参数
     * @return
     */
    protected abstract Map<String, String> generatorMessage(PropConvertConfig propConvertConfig);


}
